{include file="common/_header"}
<body>
<div class="x-body">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>选择用户
            </label>
            <div class="layui-input-inline">
                <select name="user" class="layui-select" lay-verify="user" lay-filter="user">
                    <option value="0">——请选择——</option>
                    {volist name="data.group" id="vo"}
                    <option value="{$vo.group_id}">{$vo.group_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">
                拥有权限——模块
            </label>
            <table  class="layui-table layui-input-block">
                <tbody>
                {volist name="menu" id="vo"}
                <tr>
                    <td style="width: 150px;">
                        <input name="id[]" lay-skin="primary" lay-filter="menu" type="checkbox" value="{$vo.license_id}" title="{$vo.license_name}">
                    </td>
                    <td>
                        <div class="layui-input-block">
                            {volist name="vo.subs" id="subs"}
                            {notempty name="subs.cos"}
                            <table class="layui-table layui-input-block">
                                <tr>
                                    <td style="width: 150px;">
                                        <input name="id[]" lay-skin="primary" lay-filter="menu" type="checkbox" value="{$subs.license_id}" title="{$subs.license_name}">
                                    </td>
                                </tr>
                            </table>
                            {else /}
                            <input name="id[]" lay-skin="primary" lay-filter="menu" type="checkbox" value="{$subs.license_id}" title="{$subs.license_name}">
                            {/notempty}
                            {/volist}
                        </div>
                    </td>
                </tr>
                {/volist}
                </tbody>
            </table>
        </div>
        <div class="layui-form-item">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="add">提交</button>
        </div>
    </form>
</div>
</body>
<script>

    layui.use('form', function(){
        var form = layui.form;

        form.on('select(user)', function(data){
            console.log(data.value);
            $.ajax({
                url: 'authlist',
                type: 'post',
                data: {type: 0, "group_id": data.value},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if(data["code"] == 1){
                        var boxes = document.getElementsByName("id[]");
                        for (var k = 0; k < boxes.length; k++){
                            boxes[k].checked = false;
                            form.render('checkbox');
                            for (var i = 0; i < data["data"].length; i++){
                                if(boxes[k].value == data["data"][i]){
                                    boxes[k].checked = true;
                                    break;
                                }
                            }
                        }
                        form.render('checkbox');
                    }
                }
            });
        });

        form.verify({
            user: function (value, item) {
                if(value == 0){
                    return '必填项不能为空';
                }
            }
        });

        form.on('submit(add)', function (data) {
            $.ajax({
                url: 'authlist',
                type: 'post',
                data: {type: 1, "id": data.field},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if(data["code"] == 1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000,
                        },function () {
                            window.location.reload();
                        });
                    }else{
                        layer.open({
                            title: '提交失败',
                            content: data.msg,
                            icon: 5,
                            anim: 6,
                        })
                    }
                }
            });
        });

    });
</script>
{include file="common/_bottom"}